<?php
class ArticleCatAction extends CommonAction {
	
	function _initialize(){
		parent::_initialize();
	}
	
    function index(){
    	$model = D('ArticleCat');
    	$count = $model->count('id');
    	if ($count > 0) {
    		import("ORG.Util.Page");
    		//分页查询数据
    		$voList = $model->order("`id` desc")->select();
    		//模板赋值显示
    		$this->assign('list', $voList);
    	}
    	cookie('_currentUrl_', __SELF__);
    	$this->display();
    }
    
    function add(){
    	if($this->isPost()){
	    	$model = D('ArticleCat');
	    	if (false === $model->field('cat_name')->create()) {
	    		$this->error($model->getError());
	    	}
	    	
	    	$flag = $model->add();
	    	if ($flag !== false) { //保存成功
	    		$this->success('新增成功!',cookie('_currentUrl_'));
	    	} else {
	    		//失败提示
	    		$this->error('新增失败!'.$model->getDbError());
	    	}
    	}
    	else{
    		$this->display();
    	}
    }
    
    
    function delete(){
    	$id = $this->_request('id');
    	 
    	if($id == '')
    		$this->error('参数错误!');
    	 
    	$count = D('Article')->where(array('id'=>$id))->count('id');
    	if($count)
    		$this->error('分类下有文章，不能删除!');
    	
    	$model = D('ArticleCat');
    	$flag = $model->delete($id);
    	
    	if(!$flag)
    		$this->error('删除失败!');
    	else
    		$this->success('删除成功!',cookie('_currentUrl_'));
    	
    }
    
    
}